package com.sh.learn.extjs.controller;

import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.sh.common.extjs.CUDResult;
import com.sh.common.extjs.GetResult;
import com.sh.common.page.QueryResult;
import com.sh.common.web.ResponseHandle;
import com.sh.learn.extjs.domain.Employee;
import com.sh.learn.extjs.service.EmployeeService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Controller
public class DemoController {

	@Resource(name="employeeService")
	private EmployeeService employeeService;

	@RequestMapping(value = "/javascript/{view}", method = RequestMethod.GET)
	public String javascript(@PathVariable("view") String view) {
		// base
		// bom
		return "/javascript/" + view;
	}

	@RequestMapping(value = "/extjs/{view}", method = RequestMethod.GET)
	public String base(Locale locale, Model model, @PathVariable("view") String view) {
		//base
		//event
		//core
		//practice
		//panel
		//viewport
		//window
		//dialog
		//layout
		//tabpanel
		//gridpanel
		//treepanel
		//manu
		//toolbar
		//formpanel
		//element
		return "/extjs/" + view;
	}

	@RequestMapping(value = "/data/{view}", method = RequestMethod.POST)
	public String data(Locale locale, Model model, @PathVariable("view") String view) {
		return "/extjs/" + view;
	}

	@InitBinder
	public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(df, true));
	}

	@RequestMapping(value = "/example/manage", method = RequestMethod.GET)
	public String manage() {
		return "manage";
	}

	@RequestMapping(value = "/example/manage1", method = RequestMethod.GET)
	public String manage1() {
		return "manage1";
	}

	@RequestMapping(value = "/example/list", method = RequestMethod.GET)
	public void list(HttpServletResponse response, PrintWriter out) throws Exception{
		QueryResult<Employee> result = employeeService.list("", null);
		String json = ResponseHandle.handleJson("", "", result);
		response.setCharacterEncoding("UTF-8");
		out.write(json);
		out.flush();
		out.close();
	}

	@RequestMapping(value = "/example/page", method = RequestMethod.POST)
	public void page(@RequestParam(value="start") Integer start, @RequestParam(value="limit") Integer limit, HttpServletResponse response, PrintWriter out) throws Exception{
		QueryResult<Employee> result = employeeService.page("", null, start, limit);
		String json = ResponseHandle.handleJson("", "", result);
		response.setCharacterEncoding("UTF-8");
		out.write(json);
		out.flush();
		out.close();
	}

	@RequestMapping(value = "/example/get", method = RequestMethod.GET)
	public void get(@RequestParam(value="id") Long id, HttpServletResponse response, PrintWriter out) throws Exception{
		Employee result = employeeService.get(id);
		boolean success = false;
		if(result != null){
			success = true;
		}
		String json = ResponseHandle.handleJson("", "", new GetResult(success, result));
		response.setCharacterEncoding("UTF-8");
		out.write(json);
		out.flush();
		out.close();
	}

	@RequestMapping(value = "/example/edit", method = RequestMethod.POST)
	public void edit(Employee em, HttpServletResponse response, PrintWriter out) throws Exception{
		boolean result = true;
		try {
			Long id = em.getId();
			if(id != null && !id.equals(new Long(0))){
				employeeService.update(em);
			}else{
				employeeService.add(em);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		}
		String json = ResponseHandle.handleJson("", "", new CUDResult(result));
		response.setCharacterEncoding("UTF-8");
		out.write(json);
		out.flush();
		out.close();
	}

	@RequestMapping(value = "/example/remove", method = RequestMethod.POST)
	public void remove(@RequestParam(value="id") Long id, HttpServletResponse response, PrintWriter out) throws Exception{
		boolean result = true;
		try {
			employeeService.remove(id);
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		}
		String json = ResponseHandle.handleJson("", "", new CUDResult(result));
		response.setCharacterEncoding("UTF-8");
		out.write(json);
		out.flush();
		out.close();
	}

	public EmployeeService getEmployeeService() {
		return employeeService;
	}

	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}


}
